package com.springmvc.utils.majiang;

import java.util.ArrayList;
import java.util.List;

import com.springmvc.cache.CacheKey;
import com.springmvc.cache.MapCacheManager;

import io.netty.channel.Channel;

public class ChannelUtil {

	public static Channel getChannel(Long userid) {
		MapCacheManager cache = MapCacheManager.getInstance();
		return (Channel) cache.get(CacheKey.setChannelKey(userid));
	}

	public static List<Channel> getChannelList(Long roomid) {
		MapCacheManager cache = MapCacheManager.getInstance();
		List<Channel> list = new ArrayList<Channel>();
		for (int i = 1; i <= 4; i++) {
			Long userid = (Long) cache.get(CacheKey.setRoomPositionUseridKey(roomid, i));
			if (userid == null) {
				continue;
			}
			Channel channel = (Channel) cache.get(CacheKey.setChannelKey(userid));
			if (channel != null) {
				list.add(channel);
			}
		}
		return list;
	}

	public static void setChannel(Long userid, Channel channel) {
		MapCacheManager cache = MapCacheManager.getInstance();
		cache.set(CacheKey.setChannelKey(userid), channel);
	}

}
